#include<vector>
#include<iostream>
using std::vector;


class Solution {
public:
    int trap(vector<int>& height) {
        int n = height.size(), area = 0;
        int left = 1, right = n-2;
        int maxLeft = height[0], maxRight = height[n-1];
        
        for (int i = 1; i < n-1 && left <= right; i++) {
            if (maxLeft <= maxRight) {
                if (maxLeft > height[left]) area += (maxLeft - height[left]);
                else maxLeft = height[left];
                left ++;
            }
            else {
                if (maxRight > height[right]) area += (maxRight - height[right]);
                else maxRight = height[right];
                right --;
            }
        }
        return area;
    }
};


int main() {
    vector<int> input = {};
    int n;
    while (std::cin >> n) {
        input.push_back(n);
    }
    std::cout << "input: \n";
    for (auto k: input) std::cout << k << " ";
    std::cout << "\n";
    

    auto res = Solution().trap(input);
    std::cout << "result is " << res << "\n";

}